package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberDemandBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.ui.fragment.MemberDemandFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MToast;

import java.util.ArrayList;

/**
 * 会员需求
 */
public class MemberDemandActivity extends AppActivity {
    private final String[] mTitles = {"商品报价", "租赁报价"};
    private final SparseArray<String> mSearchList = new SparseArray<>();
    private ArrayList<Fragment> mFragments;
    private ActivityMemberDemandBinding mBind;

    public static void start(Context context) {
        Intent intent = new Intent(context, MemberDemandActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_member_demand);
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mFragments.add(MemberDemandFragment.newInstance(i));
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBind.vp.setOffscreenPageLimit(mTitles.length);
        mBind.vp.setAdapter(mPagerAdapter);
        mBind.tabLayout.setupWithViewPager(mBind.vp);
        mBind.tabLayout.setNormalColor(R.color.color_main_bg);
        mBind.tabLayout.setSelectColor(R.color.color_white);
        mBind.tabLayout.setNormalSize(15);
        mBind.tabLayout.setSelectSize(18);
        mBind.tabLayout.addOnTabSelectedListener(mTitles, new XCustomTabLayout.SimpleSelectedListener(){
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mBind.vp.setCurrentItem(tab.getPosition());
            }
        });
        initSearchView();
    }

    private String mSearchText = ""; // 搜索内容

    private void initSearchView() {
        mBind.activityBar.ivBack.setOnClickListener(view -> finish());
        String hint ="请输入关键词，比如需求名称等";
        mBind.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBind.activityBar.etView.setHint(hint);
        mBind.activityBar.etView.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                if (TextUtils.isEmpty(mBind.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), hint);
                    return true;
                }
                if (mSearchText.equals(mBind.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return true;
                }
                mSearchText = mBind.activityBar.etView.getText().toString().trim();
                loadSearchData();
            }
            return false;
        });
        mBind.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBind.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBind.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBind.activityBar.tvSearchDelete.setOnClickListener(v -> {
            mSearchText = "";
            mBind.activityBar.etView.setText("");
            loadSearchData();
        });
        mBind.activityBar.tvSearch.setOnClickListener(v -> {
            if (TextUtils.isEmpty(mSearchText)) {
                if (TextUtils.isEmpty(mBind.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), hint);
                    return;
                }
            }
            if (mSearchText.equals(mBind.activityBar.etView.getText().toString())) {
                MToast.showToast(getActivity(), "请输入不同的搜索内容");
                return;
            }
            mSearchText = mBind.activityBar.etView.getText().toString().trim();
            loadSearchData();
        });
        mBind.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    public String getSearchText() {
        int tabPosition = mBind.tabLayout.getSelectedTabPosition();
        return mSearchList.get(tabPosition);
    }

    private void loadSearchData() {
        int tabPosition = mBind.tabLayout.getSelectedTabPosition();
        mSearchList.put(tabPosition, mSearchText);
        MemberDemandFragment fragment = (MemberDemandFragment) mFragments.get(tabPosition);
        fragment.loadData(false);
    }
}
